package com.cvte.boe.media.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * Created by wuguitong on 2018/4/8.
 */
public abstract class RecyclerViewClickAndSelectAdapter<VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<VH> {
    private OnItemClickListener mOnItemClickListener;
    private OnItemSelectListener mOnItemSelectListener;
    @Override
    public void onBindViewHolder(final VH holder, int position) {
        holder.itemView.setTag(position);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick((int)holder.itemView.getTag());
                }
            }
        });
        holder.itemView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                if(hasFocus){
                    if (mOnItemSelectListener != null) {
                        mOnItemSelectListener.onItemSelect((int)holder.itemView.getTag());
                    }
                }
            }
        });
    }
    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.mOnItemClickListener = onItemClickListener;
    }
    public void setOnItemSelectListener(OnItemSelectListener onItemSelectListener){
        this.mOnItemSelectListener = onItemSelectListener;
    }
    public interface OnItemClickListener {
        void onItemClick(int position);
    }
    public interface  OnItemSelectListener{
        void onItemSelect(int position);
    }
}
